package fr.insee.solr.model.annotation;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import fr.insee.solr.service.impl.SolrInseeException;
import fr.insee.solr.utils.ClassUtil;

public class CachAnnotation {
	
	private static Map<Class<?>, Field[]> beanSolrFields = new HashMap<Class<?>, Field[]>();
	private static Map<Class<?>, Field[]> beanSolrField = new HashMap<Class<?>, Field[]>();
	
	public static Field[] getSolrfields(Class<?> bean) throws SolrInseeException{
		Field[] fields = beanSolrFields.get(bean);
		if(fields == null){
			fields = loadSolrFields(bean);
		}
		return fields;
	}
	
	public static Field[] getSolrfield(Class<?> beanClass) throws SolrInseeException{
		Field[] fields = beanSolrField.get(beanClass);
		if(fields == null){
			fields = loadSolrField(beanClass);
		}
		return fields;
	}
	
	
	
	private static Field[] loadSolrField(Class<?> beanClass) throws SolrInseeException{
		Field[] fields = ClassUtil.getAnnotatedDeclaredFields(beanClass, SolrField.class, false);
		ValidateAnnotation.validate(fields);
		beanSolrField.put(beanClass, fields);
		return fields;
	}
	
	private static Field[] loadSolrFields(Class<?> beanClass) throws SolrInseeException{
		Field[] fields = ClassUtil.getAnnotatedDeclaredFields(beanClass, SolrFields.class, false);
		ValidateAnnotation.validateArray(fields);
		beanSolrFields.put(beanClass, fields);
		return fields;
	}
}
